package service;

import dao.IScheduleDao;
import dao.ITicketDao;
import dao.ScheduleDaoImpl;
import dao.TicketDaoImpl;
import entity.CinemaHall;
import entity.Movie;
import entity.Schedule;
import entity.Ticket;
import util.IDGenerator;

import java.util.Date;
import java.util.List;

public class ScheduleServiceImpl implements IScheduleService {
    private IScheduleDao scheduleDao = new ScheduleDaoImpl();
    private ITicketDao ticketDao = new TicketDaoImpl();
    public CinemaHall[] cinemaHalls = {
            new CinemaHall("H000001", "黄金厅", 8, 10),
            new CinemaHall("H000002", "白银厅", 10, 12),
            new CinemaHall("H000003", "翡翠厅", 7, 8),
            new CinemaHall("H000004", "钻石厅", 7, 8),
            new CinemaHall("H000005", "宝石厅", 8, 9)
    };

    @Override
    public boolean addSchedule(Schedule schedule) {
        for (Schedule s : scheduleDao.getAllSchedules()) {
            if (s.getCinemaHall().getHallId().equals(schedule.getCinemaHall().getHallId()) &&
                    s.getStartTime().equals(schedule.getStartTime())) {
                return false;
            }
        }
        return scheduleDao.addSchedule(schedule);
    }

    @Override
    public boolean deleteSchedule(String scheduleId) {
        Schedule schedule = scheduleDao.getScheduleById(scheduleId);
        if (schedule != null) {
            for (Ticket ticket : ticketDao.getTicketsByScheduleId(scheduleId)) {
                if (ticket.getStatus().equals("已售出")) {
                    return false;
                }
            }
            return scheduleDao.deleteSchedule(scheduleId);
        }
        return false;
    }

    @Override
    public boolean updateSchedule(Schedule schedule) {
        Schedule existingSchedule = scheduleDao.getScheduleById(schedule.getScheduleId());
        if (existingSchedule != null) {
            if (!existingSchedule.getMovie().getMovieId().equals(schedule.getMovie().getMovieId())) {
                for (Ticket ticket : ticketDao.getTicketsByScheduleId(schedule.getScheduleId())) {
                    if (ticket.getStatus().equals("已售出")) {
                        return false;
                    }
                }
            }
            return scheduleDao.updateSchedule(schedule);
        }
        return false;
    }

    @Override
    public Schedule getScheduleById(String scheduleId) {
        return scheduleDao.getScheduleById(scheduleId);
    }

    @Override
    public List<Schedule> getAllSchedules() {
        return scheduleDao.getAllSchedules();
    }

    @Override
    public List<Schedule> getSchedulesByDate(Date date) {
        return scheduleDao.getSchedulesByDate(date);
    }

    @Override
    public List<Schedule> getSchedulesByMovieId(String movieId) {
        return scheduleDao.getSchedulesByMovieId(movieId);
    }

    @Override
    public void autoGenerateSchedule(List<Movie> movies, Date startDate, Date endDate) {
        for (Movie movie : movies) {
            for (CinemaHall hall : cinemaHalls) {
                Date currentDate = startDate;
                while (!currentDate.after(endDate)) {
                    Schedule schedule = new Schedule();
                    schedule.setScheduleId(IDGenerator.generateScheduleId());
                    schedule.setMovie(movie);
                    schedule.setCinemaHall(hall);
                    schedule.setStartTime(currentDate);
                    schedule.setPrice(50.0);

                    if (movie.getRating() >= 8.0) {
                        schedule.setPrice(60.0);
                    } else if (movie.getRating() < 7.0) {
                        schedule.setPrice(40.0);
                    }

                    addSchedule(schedule);

                    long diffInMillis = 2 * 60 * 60 * 1000;
                    currentDate = new Date(currentDate.getTime() + diffInMillis);
                }
            }
        }
    }
}